package com.example.springstudy.controller;

import com.example.springstudy.base.Result;
import com.example.springstudy.base.annotation.SaveLog;
import com.example.springstudy.dao.dto.RoleDto;
import com.example.springstudy.entity.Role;
import com.example.springstudy.mapper.RoleMapper;
import com.example.springstudy.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @Autowired
    private RoleMapper roleMapper;

    @GetMapping("/userRole")
    public Result<?> getRoles(@RequestParam(required = true) Long userId){
        return Result.success(roleService.getRoles(userId));
    }

    @GetMapping("/roleList")
    public Result<?> getRoleList(){
        return Result.success(roleService.getRoleList());
    }

    @SaveLog("修改角色")
    @PostMapping("/addOrUpdate")
    public Result<?> addOrUpdate(@RequestBody RoleDto role){
        Assert.hasText(role.getName(), "角色名称不能为空！");
        Assert.hasText(role.getCode(), "角色编码不能为空！");
        return Result.success(roleService.addOrUpdateRole(role));
    }

    @SaveLog("删除角色")
    @DeleteMapping("/delete")
    public Result<Integer> delete(@RequestParam Integer roleId){
        Role role = new Role();
        role.setId(roleId);
        role.setIsDeleted(1);
        return Result.success(roleMapper.updateById(role));
    }
}
